package com.js.auth.server.module.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.js.auth.server.module.client.entity.GatewayRoute;
import com.js.auth.server.module.client.mapper.GatewayRouteMapper;
import com.js.auth.server.module.client.service.GatewayRouteService;
import com.js.common.constant.RedisKeyConstants;
import com.js.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author： ygl
 * @date： 2018/10/27
 * @Description：
 */
@Service
public class GatewayRouteServiceImpl extends BaseServiceImpl<GatewayRoute> implements GatewayRouteService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private GatewayRouteMapper gatewayRouteMapper;

    /**
     * 更新路由信息
     * @param entity
     * @return
     */
    @Override
    public int updateSelectiveById(GatewayRoute entity) {
        int rows = super.updateSelectiveById(entity);
        updateGatewayRouteCache();
        return rows;
    }

    /**
     * 更新缓存中的路由信息
     */
    public void updateGatewayRouteCache() {
        List<GatewayRoute> gatewayRoutes = selectAll();
        redisTemplate.opsForValue().set(RedisKeyConstants.ZUUL_ROUTE_KEY, JSON.toJSONString(gatewayRoutes));
    }
}
